package com.yk.filedownload.DataTransfer.RedisOp.impl;

import com.yk.filedownload.ConfigIt.Configuration;
import com.yk.filedownload.DataTransfer.RedisOp.RedisOperation;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Project Name:cykfiledownload
 * File Name:RedisOperationImpl
 * Package Name:com.yk.filedownload.DataTransfer.RedisOp.impl
 * Date:2017\12\7 0007
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */
public class RedisOperationImpl implements RedisOperation {

    private JedisCluster jedisCluster;

    private static JedisPoolConfig pool;

    private static Set<HostAndPort> nodes;

    static {
        /**
         *文件配置
         */
        String url = Configuration.REDIS_CLUSTERS;
        pool = new JedisPoolConfig();
        String[] urls = url.split(",");
        nodes = new LinkedHashSet<>();
        for (String s : urls) {
            String[] single_url = s.split(":");
            nodes.add(new HostAndPort(single_url[0],Integer.valueOf(single_url[1].trim())));
        }
        /**
         * 最大连接数
         */
        pool.setMaxTotal(Configuration.REDIS_MAXTOTAL);
        /**
         * 最大空闲数
         */
        pool.setMaxIdle(Configuration.REDIS_MAXIDLE);
        /**
         * 最大允许等待时间，如果超过这个时间会报JedisException (毫秒)
         */
        pool.setMaxWaitMillis(Configuration.REDIS_MAXWAITMILLIS);
    }

    public RedisOperationImpl(){
        jedisCluster = new JedisCluster(nodes,pool);
    }

    /**
     * 新增
     * @param key
     * @param value
     * @return
     */
    @Override
    public boolean add(String key, String value) {
        boolean temp = true;
        try {
            jedisCluster.set(key,value);
        }catch (Exception e){
            temp = false;
        }
        return temp;
    }

    @Override
    public boolean add(String key, byte[] value) {
        boolean temp = true;
        try {
            jedisCluster.set(key.getBytes("ISO-8859-1"),value);
        }catch (Exception e){
            temp = false;
        }
        return temp;
    }

    /**
     * 删除
     * @param key
     * @return
     */
    @Override
    public boolean delete(String key) {
        boolean temp = true;
        try {
            jedisCluster.del(key);
        }catch (Exception e){
            temp = false;
        }
        return temp;
    }

    @Override
    public String findByKeyString(String key) {
        return jedisCluster.get(key);
    }
    /**
     * 单查
     * @param key
     * @return
     */
    @Override
    public byte[] findByKeyByte(String key)  {
        byte[] out = null;
        try {
            out =  jedisCluster.get(key.getBytes("ISO-8859-1"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return out;
    }


    @Override
    public void closeConn() {
        try {
            jedisCluster.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
